import axios from "axios";
import _ from "lodash";
const MUTATION_TYPES = {
  DATA: "DATA",
  BOARD: "BOARD",
  RULES: "RULES",
  ACTIONS: "ACTIONS",
  ENTRIES: "ENTRIES",
  LOCALIZE: "LOCALIZE",
  FORM_ITEM_DISABLED: "FORM_ITEM_DISABLED",
  OPER_STATUS: "OPER_STATUS"
};

class FormButton {
  constructor({ name, type, url }) {
    this.name = name;
    this.type = type;
    this.url = url;
  }
}

const state = {
  data: {},
  rules: {},
  board: {},
  actions: [],
  entries: [],
  formItemDisabled: [],
  operStatus: null,
  localize: {}
};
const getters = {
  menuId (state) {
    return state.board?.menuId;
  },
  resourceId (state) {
    return state.board?.resourceId;
  },
  dataId (state) {
    return state.board?.dataId;
  },
  dataUrl (state) {
    let dataId = state.board?.dataId;
    let url = state.submit?.url;
    return dataId ? url + "/" + dataId : url;
  },
  preset (state) {
    // 预设值合并组件默认值和localize.preset
    let defaultValue =
      Object.fromEntries(
        state.entries
          ?.filter(({ value }) => value)
          .map(({ name, value }) => {
            return [name, value];
          })
      ) || {};

    let preset = _.cloneDeep(state.localize?.preset || {});
    return { ...defaultValue, ...preset };
  }
};

const mutations = {
  [MUTATION_TYPES.DATA] (state, data) {
    state.data = data;
  },
  [MUTATION_TYPES.ACTIONS] (state, actions) {
    state.actions = actions;

    actions.forEach((action) => {
      if (action.type === "submit") {
        state.submit = action;
      }
    });
  },
  [MUTATION_TYPES.ENTRIES] (state, entries) {
    state.entries = entries;
  },
  [MUTATION_TYPES.LOCALIZE] (state, localize) {
    state.localize = localize;
  },
  [MUTATION_TYPES.RULES] (state, rules) {
    state.rules = rules;
  },
  [MUTATION_TYPES.BOARD] (state, board) {
    state.board = board;
  },
  [MUTATION_TYPES.FORM_ITEM_DISABLED] (state, formItemDisabled) {
    state.formItemDisabled = formItemDisabled;
  },
  [MUTATION_TYPES.OPER_STATUS] (state, operStatus) {
    state.operStatus = operStatus;
  }
};

const actions = {
  async initialize ({ commit, dispatch, getters }, { resource, board }) {
    let { entries, actions, localize, rules } = resource;
    commit(MUTATION_TYPES.BOARD, board);
    commit(MUTATION_TYPES.RULES, rules);
    commit(MUTATION_TYPES.ACTIONS, actions);
    commit(MUTATION_TYPES.LOCALIZE, localize);
    commit(MUTATION_TYPES.ENTRIES, entries);
    commit(MUTATION_TYPES.DATA, getters.preset);
    dispatch("query");
  },
  async query ({ commit, getters }) {
    if (!getters.dataId) {
      return;
    }
    let response = await axios.get(getters.dataUrl);
    if (response.code === 0) {
      commit(MUTATION_TYPES.DATA, response.data);
      // TODO: 这里为会么要重新初始化表单结构？
      // commit(MUTATION_TYPES.ENTRIES, [...state.entries])
    }
  },
  async submit ({ getters }, data) {
    if (getters.dataId) {
      return axios.put(getters.dataUrl, data);
    } else {
      return axios.post(getters.dataUrl, data);
    }
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
